import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

/**
 * Vite配置文件
 * 补充WebSocket代理，解决跨域问题
 */
export default defineConfig({
  plugins: [vue()],
  define: {
    global: 'window'
  },
  server: {
    host: '127.0.0.1',
    port: '8866',
    https: false,
    open: true,
    historyApiFallback: true,
    // 代理配置（新增WebSocket代理）
    proxy: {
      // 已有的HTTP API代理（保持不变）
      '/platformLogin': {
        target: 'http://localhost:8001',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/platformLogin/, '')
      },
      '/schoolLogin': {
        target: 'http://localhost:8002',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/schoolLogin/, '')
      },
      '/userLogin': {
        target: 'http://localhost:8003',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/userLogin/, '')
      },
      '/studentLogin': {
        target: 'http://localhost:8004',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/studentLogin/, '')
      },
      '/platformSchool': {
        target: 'http://localhost:8011',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/platformSchool/, '')
      },
      '/schoolTeacher': {
        target: 'http://localhost:8012',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/schoolTeacher/, '')
      },
      '/schoolStudent': {
        target: 'http://localhost:8022',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/schoolStudent/, '')
      },
      '/userParentMychild': {
        target: 'http://localhost:8013',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/userParentMychild/, '')
      },
      '/userTeacherSubject': {
        target: 'http://localhost:8023',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/userTeacherSubject/, '')
      },
      '/userGroupChat': {
        target: 'http://localhost:8033',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/userGroupChat/, '')
      },

      // 新增：WebSocket代理（关键配置）
      '/ws': {
        target: 'ws://localhost:8033', // 后端WebSocket服务地址（注意用ws协议）
        changeOrigin: true,
        ws: true, // 启用WebSocket代理（必须设置）
        rewrite: path => path // 不修改路径，直接转发/ws到后端/ws
      },
      '/userForum': {
          target: 'http://localhost:8043',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/userForum/, '')
      },'/course': {
          target: 'http://localhost:8100',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/course/, '')
      },
      '/homework': {
          target: 'http://localhost:8101',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/homework/, '')
      }
    }
  }
})